package com.nie.orderfood.service.impl;

import com.nie.orderfood.DTO.OrderMasterDTO;
import com.nie.orderfood.enums.ResultEnum;
import com.nie.orderfood.exception.SellException;
import com.nie.orderfood.service.BuyerService;
import com.nie.orderfood.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

/**
 * @program: orderfood
 * @description: BuyerService的实现类
 * @author: NieMiao
 * @create: 2018-10-17 09:25
 **/
public class BuyerServiceImpl implements BuyerService {
    @Autowired
    private OrderService orderService;

    @Override
    public OrderMasterDTO findOrderOne(String openid, String orderId) {
        return decideOwnerOrder(openid,orderId);
    }

    @Override
    public OrderMasterDTO cancelOrder(String openid, String orderId) {
        OrderMasterDTO orderMasterDTO = decideOwnerOrder(openid, orderId);
        if (orderMasterDTO==null) {
            throw new SellException(ResultEnum.ORDER_NOT_EXIST_ERROR);
        }
        return orderMasterDTO;
    }

    @Override
    public OrderMasterDTO decideOwnerOrder(String openid, String orderId) {
        OrderMasterDTO orderMasterDTO = orderService.findOne(orderId);
        if (orderMasterDTO==null) {
            return null;
        }
        if (!orderMasterDTO.getBuyerOpenid().equalsIgnoreCase(openid)) {
            throw new SellException(ResultEnum.ORDER_OPENID_ERROR);
        }
        return orderMasterDTO;
    }
}
